﻿@using DCMS.Web.Extensions;
@using DCMS.Web.Framework;
@using DCMS.Web.Framework.UI;
@using DCMS.ViewModel.Models.Finances;
@using DCMS.ViewModel.Models.Sales;
@using System.Collections.Generic;
@using System.Linq;
@using DCMS.Core.Domain.Common;

@model SaleReportItemListModel

<section id="content_wrapper">

	<!-- Start: Topbar-Dropdown -->
	<div id="topbar-dropmenu">
		@await Html.PartialAsync("_ToolBox")
	</div>
	<!-- End: Topbar-Dropdown -->
	<!-- Start: Topbar -->
	<header id="topbar">
		<div class="topbar-left">
			<ol class="breadcrumb">
				<li class="crumb-active">
					<a href="#">销售</a>
				</li>
				<li class="crumb-icon">
					<a href="@Url.RouteUrl("HomePage")">
						<span class="glyphicon glyphicon-home"></span>
					</a>
				</li>
				<li class="crumb-link">
					<a href="#">销售报表</a>
				</li>
				<li class="crumb-trail">销售明细表</li>
			</ol>
		</div>
		<div class="topbar-right">
			<div class="ml15 ib va-m" id="toggle_sidemenu_r">
				<a href="#" class="pl5">
					<i class="fa fa-sign-in fs22 text-primary"></i>
					<span class="badge badge-hero badge-danger">3</span>
				</a>
			</div>
		</div>
	</header>
	<!-- End: Topbar -->
	<!-- Begin: Content -->
	<section id="content" class="formSection">

		<div class="special-alerts">
			<div class="alert alert-danger light alert-dismissable" id="alert-demo-2" style="display: none;">
				<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
				<i class="fa fa-info pr10"></i>
				<strong>您没有被授权此操作!</strong>
			</div>
		</div>

		<div class="row">
			<div class="col-md-12">

				<form asp-action="SaleReportItem" asp-controller="SaleReport" method="get" id="formSearch" novalidate="novalidate">

					<div class="panel panel-default" id="panel0">
						<div class="panel-heading">
							<span class="glyphicon glyphicon-search"></span> 高级搜索
							<a class="btn btn-default btn-sm pull-right m5 mrn p5 pbn" id="AdvancedSearch"><i class="fa fa-angle-double-right"></i></a>
						</div>
						<div class="panel-body p10">

							<div class="row">
								<div class="col-sm-2">
									<div class="input-group">
										<input asp-for="ProductId" type="hidden" />
										<input asp-for="ProductName" class="form-control" placeholder="商品名称..." />
										<span class="input-group-btn">
											<button id="btn_search_product" type="button" class="btn btn-default">
												<span class="glyphicon glyphicon-search"></span>
											</button>
										</span>
									</div>
								</div>
								<div class="col-sm-2">
									<dcms-select asp-for="CategoryId" asp-items="@Model.Categories" asp-default="商品类别" />
								</div>
								<div class="col-sm-2">
									<dcms-select asp-for="BrandId" asp-items="@Model.Brands" asp-default="品牌" />
								</div>
								<div class="col-sm-2">
									<div class="input-group">
										<input asp-for="TerminalId" type="hidden" />
										<input asp-for="TerminalName" class="form-control" placeholder="客户..." />
										<span class="input-group-btn">
											<button id="btn_search_customer" type="button" class="btn btn-default">
												<span class="glyphicon glyphicon-search"></span>
											</button>
										</span>
									</div>
								</div>
								<div class="col-sm-2">
									<input asp-for="BillNumber" class="form-control" placeholder="单据编号..." />
								</div>
								<div class="col-sm-2">
									<dcms-select asp-for="SaleTypeId" asp-items="@Model.SaleTypes" asp-default="销售商品类别" />
								</div>

							</div>

							<div class="row mt10 mb10">
								<div class="col-sm-2">
									<dcms-select asp-for="BusinessUserId" asp-items="@Model.BusinessUsers" asp-default="业务员" />
								</div>
								<div class="col-sm-2">
									<dcms-select asp-for="WareHouseId" asp-items="@Model.WareHouses" asp-default="仓库" />
								</div>
								<div class="col-sm-2">
									<dcms-select asp-for="PayTypeId" asp-items="@Model.PayTypes" asp-default="付款类型" />
								</div>
								<div class="col-sm-2">
									<dcms-select asp-for="DeliveryUserId" asp-items="@Model.DeliveryUsers" asp-default="送货员" />
								</div>
								<div class="col-sm-2">
									<dcms-select asp-for="RankId" asp-items="@Model.Ranks" asp-default="客户等级" />
								</div>
								<div class="col-sm-2">
									<input asp-for="Remark" class="form-control" placeholder="备注..." />
								</div>
							</div>

							<div class="row">
								<div class="col-sm-2">
									<dcms-select asp-for="ChannelId" asp-items="@Model.Channels" asp-default="渠道" />
								</div>
								<div class="col-sm-2">
									<dcms-editor asp-for="StartTime" id="StartTime" placeholder="开始..." />
								</div>
								<div class="col-sm-2">
									<dcms-editor asp-for="EndTime" id="EndTime" placeholder="结束..." />
								</div>
								<div class="col-sm-2">
									<dcms-select-bool asp-for="CostContractProduct" asp-items="@(new string[] {  "是", "否"})" asp-default="费用合同兑现商品" />
								</div>
								<div class="col-sm-2">
									<dcms-select asp-for="DistrictId" asp-items="@Model.Districts" asp-default="片区" />
								</div>
								<div class="col-sm-2">
									<dcms-select-bool asp-for="AuditedStatus" asp-items="@(new string[] {  "已审核", "未审核"})" asp-default="审核状态" />
								</div>
								
							</div>
							<div class="row mt10 mb10">
								<div class="col-sm-12">
									<button id="btn_query" type="submit" class="btn btn-primary btn-block">
										<span class="glyphicon glyphicon-search mr5" aria-hidden="true"></span>查询
									</button>
								</div>
							</div>
						</div>
					</div>

				</form>

				<div class="panel">
					<div class="panel-heading">
						<div class="panel-title">
							<span class="glyphicon glyphicon-tasks"></span> 报表
						</div>
					</div>
					<div class="panel-body p10">
						<div class="bootstrap-table">

							<div id="toolbar" class="btn-group mb10 mt10">
								<a id="btn_export" class="btn btn-info btn-sm mr5">
									<span class="glyphicon glyphicon-export mr5"></span>导出
								</a>
								@*<button id="btn_print" type="button" class="btn btn-alert btn-sm mr5">
				<span class="glyphicon glyphicon-print mr5"></span>打印
			</button>*@
								<a href="@Url.Action("SaleReportItem")" class="btn btn-default btn-sm">
									<span class="glyphicon glyphicon-refresh mr5"></span>重置
								</a>
							</div>

							<div class="fixed-table-container">
								<div class="fixed-table-body" id="tableDiv" style="overflow-x:auto;">
									<table class="table table-striped table-bordered table-hover">

										<thead>
											<tr class="datagrid-header-row">
												<th class="text-center w150"><div class="th-inner">单据编号</div></th>
												<th class="text-left"><div class="th-inner">单据类型</div></th>
												<th class="text-left"><div class="th-inner">客户</div></th>
												@*<th class="text-left"><div class="th-inner">客户编码</div></th>*@
												<th class="text-left"><div class="th-inner">业务员</div></th>
												<th class="text-left"><div class="th-inner">送货员</div></th>
												<th class="text-center"><div class="th-inner">交易时间</div></th>
												<th class="text-center"><div class="th-inner">审核时间</div></th>
												<th class="text-left"><div class="th-inner">仓库</div></th>
												@*<th class="text-left"><div class="th-inner">商品编号</div></th>*@
												<th class="text-left"><div class="th-inner">商品名称</div></th>
												<th class="text-left"><div class="th-inner">商品类型</div></th>
												<th class="text-left"><div class="th-inner">商品品牌</div></th>
												@*<th class="text-left"><div class="th-inner">条形码</div></th>*@
												<th class="text-left"><div class="th-inner">单位换算</div></th>
												<th class="text-center"><div class="th-inner">数量</div></th>
												<th class="text-left"><div class="th-inner">单位</div></th>
												<th class="text-right"><div class="th-inner">单价</div></th>
												<th class="text-right"><div class="th-inner">金额</div></th>
												<th class="text-right"><div class="th-inner">成本金额</div></th>
												<th class="text-right"><div class="th-inner">利润</div></th>
												@*<th class="text-right"><div class="th-inner">成本利润率</div></th>*@
												<th class="text-right"><div class="th-inner">系统价</div></th>
												<th class="text-right"><div class="th-inner">变动差额</div></th>
												<th class="text-right"><div class="th-inner">预设进价</div></th>
												@*<th class="text-right"><div class="th-inner">最近采购价</div></th>
												<th class="text-right"><div class="th-inner">最近结算成本价</div></th>*@
												<th class="text-left"><div class="th-inner">备注</div></th>

											</tr>
										</thead>

										<tbody>
											@{
												if (Model.Items != null && Model.Items.Count > 0)
												{
													@foreach (var item in Model.Items)
													{
											<tr class="datagrid-header-row">
												<td class="text-center">
													@if (item.BillTypeId == 1)
													{
														if (item.BillId != null && item.BillId != 0)
														{

															<a href="@Url.Action("Edit","SaleBill",new { id= item.BillId})" target="_blank"> @item.BillNumber </a>

														}
														if (item.ReservationBillId != null && item.ReservationBillId != 0)
														{

															<a href="@Url.Action("Edit","SaleReservationBill",new { id= item.ReservationBillId})" target="_blank"> @item.ReservationBillNumber </a>

														}
													}
													else if (item.BillTypeId == 2)
													{
														if (item.BillId != null && item.BillId != 0)
														{
															<a href="@Url.Action("Edit","ReturnBill",new { id= item.BillId})" target="_blank">@item.BillNumber</a>
														}
														if (item.ReservationBillId != null && item.ReservationBillId != 0)
														{
															<a href="@Url.Action("Edit","ReturnReservationBill",new { id= item.ReservationBillId})" target="_blank">@item.ReservationBillNumber</a>
														}
													}
												</td>
												<td class="text-left">@item.BillTypeName</td>
												<td class="text-left">@item.TerminalName</td>
												@*<td class="text-left">@item.TerminalCode</td>*@
												<td class="text-left">@item.BusinessUserName</td>
												<td class="text-left">@item.DeliveryUserName</td>
												<td class="text-center">@item.TransactionDate</td>
												<td class="text-center">@item.AuditedDate</td>
												<td class="text-left">@item.WareHouseName</td>
												@*<td class="text-left">@item.ProductSKU</td>*@
												<td class="text-left">
													<a href="#" alt="@item.BarCode" title="@item.BarCode">
														@item.ProductName
													</a>
												</td>
												<td class="text-left">@item.CategoryName</td>
												<td class="text-left">@item.BrandName</td>
												@*<td class="text-left">@item.BarCode</td>*@
												<td class="text-left">@item.UnitConversion</td>
												<td class="text-right">@item.Quantity</td>
												<td class="text-left">@item.UnitName</td>
												<td class="text-right">￥ @item.Price.Value.ToString("0.00")</td>
												<td class="text-right">￥ @item.Amount.Value.ToString("0.00")</td>
												<td class="text-right">￥ @item.CostAmount.Value.ToString("0.00")</td>
												<td class="text-right">@item.Profit.Value.ToString("0.00")</td>
												@*<td class="text-right">@(((item.CostProfitRate ?? 0)).ToString("0.00"))%</td>*@
												<td class="text-right">￥ @item.SystemPrice</td>
												<td class="text-right">@item.ChangeDifference</td>


												<td class="text-right">￥ @item.PresetPrice</td>
												@*<td class="text-right">￥ @item.RecentPurchasesPrice</td>
												<td class="text-right">￥ @item.RecentSettlementCostPrice</td>*@
												<td class="text-left">@item.Remark</td>

											</tr>
													}

										<tr>
											<td></td>
											<td></td>
											<td class="text-right"><b>小计</b></td>
											<td class="text-right" colspan="11"><b>@(Model.PageSumQuantityConversion)</b></td>
											<td></td>

											<td class="text-right"><span style="color:#ffd700;"><b>￥</b></span><b>@(Model.PageSumAmount)</b></td>
											<td class="text-right"><span style="color:#ffd700;"><b>￥</b></span><b>@(Model.PageSumCostAmount)</b></td>
											<td class="text-right"><span style="color:#ffd700;"><b>￥</b></span><b>@(Model.PageSumProfit)</b></td>
											@*<td class="text-right"><b>@(((Model.PageSumCostProfitRate ?? 0)).ToString("0.00"))%</b></td>*@
											<td></td>
											<td class="text-right"><b>@(Model.PageSumChangeDifference)</b></td>

											@*<td colspan="4"></td>*@
											<td></td>
											<td></td>
										</tr>

										<tr>
											<td></td>
											<td></td>
											<td class="text-right"><b>合计</b></td>
											<td class="text-right" colspan="11"><b>@(Model.TotalSumQuantityConversion)</b></td>
											<td></td>

											<td class="text-right"><span style="color:#ffd700;"><b>￥</b></span><b>@(Model.TotalSumAmount)</b></td>
											<td class="text-right"><span style="color:#ffd700;"><b>￥</b></span><b>@(Model.TotalSumCostAmount)</b></td>
											<td class="text-right"><span style="color:#ffd700;"><b>￥</b></span><b>@(Model.TotalSumProfit)</b></td>
											@*<td class="text-right"><b>@(((Model.TotalSumCostProfitRate ?? 0)).ToString("0.00"))%</b></td>*@

											<td></td>
											<td class="text-right"><b>@(Model.TotalSumChangeDifference)</b></td>

											@*<td colspan="4"></td>*@
											<td></td>
											<td></td>
										</tr>
												}
												else
												{
													<tr class="no-records-found"><td class="text-center" colspan="24">没有找到匹配的记录</td></tr>
												}
											}
										</tbody>
									</table>

								</div>
							</div>

							@{
								var pager = Html.Pager(Model.PagingFilteringContext).QueryParam("pagenumber");
							}

							@if (!pager.IsEmpty())
							{
								<div class="fixed-table-pagination" style="display: block;">
									@Html.Raw(pager.ToString())
								</div>
							}
						</div>

					</div>
				</div>
			</div>
		</div>

	</section>
	<!-- End: Content -->
</section>

@await Html.PartialAsync("_Windows", Model)

@section CurPageScripts
{

<script type="text/javascript">

	jQuery(document).ready(function () {

		"use strict";

		// Init Theme Core
		Core.init();

		// Init Demo JS
		Demo.init();

		//权限提示
		if ($(this).getUrlQueryString("Authorize") == "noAuthorize") {
			$('#alert-demo-2').fadeToggle();
			setTimeout(function () {
				$('#alert-demo-2').fadeToggle();
			}, 3000);
		}

		//高级搜索
		$("#panel0 #AdvancedSearch").click(function () {
			var ico = $(this).find(".fa");
			if (ico.hasClass('fa-angle-double-right'))
				ico.removeClass("fa-angle-double-right").addClass("fa-angle-double-down");
			else
				ico.removeClass("fa-angle-double-down").addClass("fa-angle-double-right");
			$("#panel0").find(".panel-body").toggleClass("hidden fadeIn");
		});

		//固定列
		$("#tableDiv").scroll(function () {
			$(this).fixedTableColumn(this, 3);
		});

		//商品列表选择赋值
		window.operateEventsPop = {
			'click .rowSelect': function (e, value, row, index) {
				var index = $(this).attr("data-id");
				var keys = Object.keys(row.Units);
				$("#ProductName").val(row.Name);
				$("#ProductId").val(row.Id);
				$('#ModalProductWindows').modal('hide');
			}
		};

		$("#btn_search_product").click(function () {
			$(this).productSelectShowModal($("#ModalProductWindows"), "/Product/AsyncSearchSelectPopup", null, "选择商品", $("#ProductModalForm"),
				function () { });
		});

		//客户搜索
		$(document).on('click', "#btn_search_customer", function () {
			var actionUrl = "/Terminal/AsyncSearchSelectPopup";
			$(this).showModalV2($("#TerminalSelectModalWindow"), actionUrl, { ids: "" }, "选择终端", $("#TerminalSelectForm"), function (rows) { });
		});

		window.operateEventsPopTerminal = {
			'click .rowSelect': function (e, value, row, index) {
				try {
					$("#TerminalId").val(row.Id);
					$("#TerminalName").val(row.Name);
					$('#TerminalSelectModalWindow').modal('hide');
				}
				catch (err) {
					console.log(err);
					$(this).showToastr(err);
					return;
				}
			}
		};

		//防止重复查询
		var queryNumber = 1;
		$("#btn_query").click(function () {
			if (queryNumber > 1) {
				return false;
			}
			queryNumber = queryNumber + 1;
		});

		//用户修改客户名称，将客户Id置零
		$("#TerminalName").bind("change", function () {
			$("#TerminalId").val(0);
		});

		//用户修改商品名称，将商品Id置零
		$("#ProductName").bind("change", function () {
			$("#ProductId").val(0);
		});
		//导出
		$("#btn_export").click(function () {
			window.location.href = "/SaleReport/ExportSaleReportItem?productId=" + $("#ProductId").val() + "&productName=" + $("#ProductName").val() + "&categoryId=" + $("#CategoryId").val() + "&brandId=" + $("#BrandId").val() + "&terminalId=" + $("#TerminalId").val() + "&terminalName=" + $("#TerminalName").val() + "&billNumber=" + $("#BillNumber").val() + "&saleTypeId=" + $("#SaleTypeId").val() + "&businessUserId=" + $("#BusinessUserId").val() + "&wareHouseId=" + $("#WareHouseId").val() + "&payTypeId=" + $("#PayTypeId").val() + "&deliveryUserId=" + $("#DeliveryUserId").val() + "&rankId=" + $("#RankId").val() + "&remark=" + $("#Remark").val() + "&channelId=" + $("#ChannelId").val() + "&startTime=" + $("#StartTime").val() + "&endTime=" + $("#EndTime").val() + "&costContractProduct=" + $("#CostContractProduct").val() + "&districtId=" + $("#DistrictId").val() + "&pagenumber=@Context.Request.Query["pagenumber"]";
		});
		//开始日期
		$('#StartTime').change(function () {
			var startTime = new Date($(this).val());
			var endTime = new Date($('#EndTime').val());
			var diffMonth = DateDiff(startTime, endTime);
			if (diffMonth > 90) {
				$(this).showToastr("最多统计3个月数据！");
				return false;
			}
		});
		//结束日期
		$('#EndTime').change(function () {
			var startTime = new Date($('#StartTime').val());
			var endTime = new Date($(this).val());
			var diffMonth = DateDiff(startTime, endTime);
			if (diffMonth > 90) {
				$(this).showToastr("最多统计3个月数据！");
				return false;
			}
		});
	});
	function DateDiff(sDate, eDate) {
		var date1 = new Date(sDate);
		var date2 = new Date(eDate);
		var date3 = date2.getTime() - date1.getTime();
		var days = Math.floor(date3 / (24 * 3600 * 1000));
		return days;
	}
</script>
}
